package com.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.common.config.exception.MyErrorException;
import com.crm.common.result.Result;
import com.crm.common.result.ResultCodeEnum;
import com.crm.dto.UserChangePasswordDto;
import com.crm.dto.UserQueryDto;
import com.crm.dto.UserSaveDto;
import com.crm.dto.UserUpdateDto;
import com.crm.model.User;
import com.crm.service.UserService;
import com.crm.vo.UserVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @version 1.0
 * @description:
 * @date 2024/6/7 16:31
 */

@Tag(name = "用户相关")
@RestController
@RequestMapping("/v1/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/save")
    @Operation(summary = "用户添加")
    public Result saveUser(@RequestBody @Validated UserSaveDto userSaveDto) {
        userService.saveUser(userSaveDto);
        return Result.ok();
    }

    @GetMapping("{id}")
    @Operation(summary = "用户详情")
    private Result<UserVo> detail(@PathVariable long id) {
        User user = userService.getById(id);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        return Result.ok(userVo);
    }

    @GetMapping("/list/{page}/{limit}")
    @Operation(summary = "用户列表")
    public Result list(@PathVariable long page, @PathVariable long limit, @ParameterObject UserQueryDto userQueryDto) {
        Page<User> userPage = new Page<>(page, limit);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();

        String keyword = userQueryDto.getKeyword();
        Integer status = userQueryDto.getStatus();
        String startTime = userQueryDto.getStartTime();
        String endTime = userQueryDto.getEndTime();

        if (StringUtils.hasText(keyword)) {
            queryWrapper.like(User::getUserName, keyword).
                    or().
                    like(User::getFullName, keyword);
        }

        if (status != null && status != 0) {
            queryWrapper.eq(User::getStatus, status);
        }

        if (StringUtils.hasText(startTime)) {
            queryWrapper.ge(User::getCreateTime, startTime);
        }
        if (StringUtils.hasText(endTime)) {
            queryWrapper.le(User::getCreateTime, endTime);
        }

        queryWrapper.orderByAsc(User::getId);
        Page<User> pageData = userService.page(userPage, queryWrapper);

        pageData.convert(result -> {
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(result, userVo);
            return userVo;
        });

        return Result.ok(pageData);

    }


    @PutMapping("/update")
    @Operation(summary = "更新用户")
    public Result update(@RequestBody @Validated UserUpdateDto userUpdateDto) {
        User user = new User();
        BeanUtils.copyProperties(userUpdateDto, user);
        user.setStatus(1);
        if (!userService.updateById(user)) {
            throw new MyErrorException(ResultCodeEnum.UPDATE_FAIL);
        }
        return Result.ok();
    }

    @PutMapping("/changePassword")
    @Operation(summary = "更改密码")
    public Result changePassword(@RequestBody @Validated UserChangePasswordDto userChangePasswordDto) {
        userService.changePassword(userChangePasswordDto);
        return Result.ok();
    }

}